import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: { requiresAuth: false }
  },
  
  // 顾客路由
  {
    path: '/customer',
    component: () => import('@/layouts/CustomerLayout.vue'),
    meta: { requiresAuth: true, role: 'customer' },
    children: [
      {
        path: '',
        redirect: '/customer/home'
      },
      {
        path: 'home',
        name: 'CustomerHome',
        component: () => import('@/views/customer/Home.vue'),
        meta: { title: '首页' }
      },
      {
        path: 'products',
        name: 'ProductList',
        component: () => import('@/views/customer/ProductList.vue'),
        meta: { title: '商品列表' }
      },
      {
        path: 'products/:id',
        name: 'ProductDetail',
        component: () => import('@/views/customer/ProductDetail.vue'),
        meta: { title: '商品详情' }
      },
      {
        path: 'cart',
        name: 'Cart',
        component: () => import('@/views/customer/Cart.vue'),
        meta: { title: '购物车' }
      },
      {
        path: 'orders',
        name: 'CustomerOrders',
        component: () => import('@/views/customer/Orders.vue'),
        meta: { title: '我的订单' }
      },
      {
        path: 'orders/:id',
        name: 'OrderDetail',
        component: () => import('@/views/customer/OrderDetail.vue'),
        meta: { title: '订单详情' }
      },
      {
        path: 'refunds',
        name: 'CustomerRefunds',
        component: () => import('@/views/customer/Refunds.vue'),
        meta: { title: '退货记录' }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/customer/Profile.vue'),
        meta: { title: '个人中心' }
      }
    ]
  },
  
  // 员工路由
  {
    path: '/employee',
    component: () => import('@/layouts/EmployeeLayout.vue'),
    meta: { requiresAuth: true, role: 'employee' },
    children: [
      {
        path: '',
        redirect: '/employee/cashier'
      },
      {
        path: 'cashier',
        name: 'Cashier',
        component: () => import('@/views/employee/Cashier.vue'),
        meta: { title: '收银台' }
      },
      {
        path: 'orders',
        name: 'EmployeeOrders',
        component: () => import('@/views/employee/Orders.vue'),
        meta: { title: '订单管理' }
      },
      {
        path: 'orders/:id',
        name: 'EmployeeOrderDetail',
        component: () => import('@/views/employee/OrderDetail.vue'),
        meta: { title: '订单详情' }
      },
      {
        path: 'products',
        name: 'EmployeeProducts',
        component: () => import('@/views/employee/Products.vue'),
        meta: { title: '商品管理' }
      },
      {
        path: 'stock',
        name: 'EmployeeStock',
        component: () => import('@/views/employee/Stock.vue'),
        meta: { title: '库存管理' }
      },
      {
        path: 'refund',
        name: 'EmployeeRefund',
        component: () => import('@/views/employee/Refund.vue'),
        meta: { title: '退货管理' }
      },
      {
        path: 'performance',
        name: 'Performance',
        component: () => import('@/views/employee/Performance.vue'),
        meta: { title: '业绩统计' }
      }
    ]
  },
  
  // 管理员路由
  {
    path: '/admin',
    component: () => import('@/layouts/AdminLayout.vue'),
    meta: { requiresAuth: true, role: 'admin' },
    children: [
      {
        path: '',
        redirect: '/admin/dashboard'
      },
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/admin/Dashboard.vue'),
        meta: { title: '数据概览' }
      },
      {
        path: 'categories',
        name: 'Categories',
        component: () => import('@/views/admin/Categories.vue'),
        meta: { title: '分类管理' }
      },
      {
        path: 'products',
        name: 'AdminProducts',
        component: () => import('@/views/admin/Products.vue'),
        meta: { title: '商品管理' }
      },
      {
        path: 'stock',
        name: 'AdminStock',
        component: () => import('@/views/admin/Stock.vue'),
        meta: { title: '库存管理' }
      },
      {
        path: 'orders',
        name: 'AdminOrders',
        component: () => import('@/views/admin/Orders.vue'),
        meta: { title: '订单管理' }
      },
      {
        path: 'orders/:id',
        name: 'AdminOrderDetail',
        component: () => import('@/views/admin/OrderDetail.vue'),
        meta: { title: '订单详情' }
      },
      {
        path: 'refunds',
        name: 'AdminRefunds',
        component: () => import('@/views/admin/Refunds.vue'),
        meta: { title: '退货管理' }
      },
      {
        path: 'employees',
        name: 'Employees',
        component: () => import('@/views/admin/Employees.vue'),
        meta: { title: '员工管理' }
      },
      {
        path: 'finance',
        name: 'Finance',
        component: () => import('@/views/admin/Finance.vue'),
        meta: { title: '财务统计' }
      }
    ]
  },
  
  // 404 页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth !== false)
  const requiredRole = to.meta.role

  if (requiresAuth) {
    if (!userStore.isLoggedIn) {
      // 未登录，跳转到登录页
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else if (requiredRole && userStore.role !== requiredRole) {
      // 角色不匹配，跳转到对应角色的首页
      const roleHomeMap = {
        customer: '/customer/home',
        employee: '/employee/cashier',
        admin: '/admin/dashboard'
      }
      next(roleHomeMap[userStore.role] || '/login')
    } else {
      next()
    }
  } else {
    // 如果已登录，访问登录/注册页面时跳转到对应角色首页
    if ((to.path === '/login' || to.path === '/register') && userStore.isLoggedIn) {
      const roleHomeMap = {
        customer: '/customer/home',
        employee: '/employee/cashier',
        admin: '/admin/dashboard'
      }
      next(roleHomeMap[userStore.role] || '/customer/home')
    } else {
      next()
    }
  }
})

export default router
